package connectx

import (
	connect "connectrpc.com/connect"
	"fmt"
	"google.golang.org/protobuf/compiler/protogen"
	"google.golang.org/protobuf/types/descriptorpb"
)

const (
	Version                    = "1.16.0"
	UnknowDefault              = "default"
	GeneratedFilenameExtension = ".connectx.supplement.pb.go"
)

const (
	ProtoreflectPackage = protogen.GoImportPath("google.golang.org/protobuf/reflect/protoreflect")
	ConnectPackage      = protogen.GoImportPath("connectrpc.com/connect")
	ContextPackage      = protogen.GoImportPath("context")
	FmtPackage          = protogen.GoImportPath("fmt")
	LogPackage          = protogen.GoImportPath("log")
	HttpPackage         = protogen.GoImportPath("net/http")
	StringPackage       = protogen.GoImportPath("strings")
	IoPackage           = protogen.GoImportPath("io")
)

// HTTP方法常量
const (
	HTTPMethodGET     = "GET"
	HTTPMethodPOST    = "POST"
	HTTPMethodPUT     = "PUT"
	HTTPMethodDELETE  = "DELETE"
	HTTPMethodPATCH   = "PATCH"
	HTTPMethodHEAD    = "HEAD"
	HTTPMethodOPTIONS = "OPTIONS"
)

func ProcedureConstName(m *protogen.Method) string {
	return fmt.Sprintf("%s%sProcedure", m.Parent.GoName, m.GoName)
}

func ProcedureHandlerName(m *protogen.Method) string {
	return fmt.Sprintf("%s%sHandler", Unexport(m.Parent.GoName), m.GoName)
}

func MethodIdempotency(method *protogen.Method) connect.IdempotencyLevel {
	methodOptions, ok := method.Desc.Options().(*descriptorpb.MethodOptions)
	if !ok {
		return connect.IdempotencyUnknown
	}
	switch methodOptions.GetIdempotencyLevel() {
	case descriptorpb.MethodOptions_NO_SIDE_EFFECTS:
		return connect.IdempotencyNoSideEffects
	case descriptorpb.MethodOptions_IDEMPOTENT:
		return connect.IdempotencyIdempotent
	case descriptorpb.MethodOptions_IDEMPOTENCY_UNKNOWN:
		return connect.IdempotencyUnknown
	}
	return connect.IdempotencyUnknown
}
